#include "ChatMessageLayer.h"
#include "InputManager.h"

#include <math.h>

using namespace cocos2d;

//void ChatMessageLayer::onEnter() {
//	(OnNextPageCallback->rec->*OnNextPageCallback->call)(NULL);
//}

void ChatMessageLayer::onExit() {
	InputManager::getSingletonPtr()->removeKeyListener("Chat Message Box");
}

bool ChatMessageLayer::init() {
	InputManager::getSingletonPtr()->addKeyListener(this, "Chat Message Box");

	_CurrentText = CCLabelBMFont::labelWithString("Place Holder", "Fonts\\pokemon.fnt");
	_CurrentText2 = CCLabelBMFont::labelWithString("Place Holder", "Fonts\\pokemon.fnt");

	_TotalPages = 1;
	_CurrentPage = 1;

	CCSprite* _chatBox1 = CCSprite::spriteWithFile("Images\\ChatBox\\chatbox_1.png");

	_chatBox1->setPositionX(400);
	_chatBox1->setPositionY(50);

	_CurrentText->setPosition(_chatBox1->getPositionX()-70, _chatBox1->getPositionY()+8);
	_CurrentText->setWidth(_chatBox1->getContentSize().width);
	_CurrentText->setScale(.5f);
	_CurrentText->setColor(ccBLACK);
	_CurrentText->setString("Hello, there!", true);

	_CurrentText2->setPosition(_chatBox1->getPositionX()-58, _chatBox1->getPositionY()-8);
	_CurrentText2->setWidth(_chatBox1->getContentSize().width);
	_CurrentText2->setScale(.5f);
	_CurrentText2->setColor(ccBLACK);
	_CurrentText2->setString("Glad to meet you!", true);

	this->addChild(_chatBox1);
	this->addChild(_CurrentText, 3);
	this->addChild(_CurrentText2, 3);

	this->scheduleUpdate();

	return true;
}

void ChatMessageLayer::menuCloseCallback(CCObject* pSender) {
	CCDirector::sharedDirector()->end();
}

void ChatMessageLayer::update(ccTime* delta) {
	InputManager::getSingletonPtr()->capture(); 
}

bool ChatMessageLayer::keyPressed( const OIS::KeyEvent &e ) {
	return true;
}

bool ChatMessageLayer::keyReleased( const OIS::KeyEvent &e ) {
	return true;
}

bool ChatMessageLayer::FinishedLoadingCurrentPage() {
	return true;
}

void ChatMessageLayer::NextPage() {
	CustomObject* tmp = new CustomObject();

	if (_CurrentPage + 1 > _TotalPages) {
		//Current page is last page
		tmp->m_ID = _TotalPages;
		(OnLastPageCallback->rec->*OnLastPageCallback->call)(tmp);
	} else {
		//Go to next page
		tmp->m_ID = _CurrentPage;
		(OnNextPageCallback->rec->*OnNextPageCallback->call)(tmp);

		_CurrentPage++;
	}
}

void ChatMessageLayer::CalculatePages() {
	_TotalPages = _MessagePages.size();

	if (_TotalPages <= 0) {
		_TotalPages = 1;
	}
}